package com.cinemtl.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import utils.MailManager;

import com.cinemtl.beans.Order;
import com.cinemtl.beans.ShoppingCart;
import com.cinemtl.beans.User;
import com.cinemtl.manager.OrderManager;
import com.google.gson.Gson;

/**
 * Servlet implementation class Checkout
 */
@WebServlet("/checkout")
public class Checkout extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Checkout() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Gson json = new Gson();
		Order order = json.fromJson(request.getParameter("order"), Order.class);
		OrderManager.insert(order);
		ShoppingCart.removeOrder(order.getIdMovie());
		HttpSession se= request.getSession();
		User user = (User) se.getAttribute("user");
		MailManager.sendOrder(user.getLname(),order,user.getEmail());
		

	}

}
